package jfox.platform.datadict.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.QueryHint;

import jfox.platform.datadict.entity.DictItem;
import jfox.platform.infrastructure.EntityObject;
import jfox.platform.infrastructure.SuperDAO;

/**
 * @author <a href="mailto:yang_y@sysnet.com.cn">Young Yang</a>
 */
@NamedNativeQueries(
        {
        @NamedNativeQuery(
                name = DictItemDAOBean.GET_DICT_ITEM_BY_ID,
                query = "SELECT * FROM T_SYS_DICT_ITEM WHERE ID=$ID",
                resultClass = DictItem.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "dictmgmt")
                }),      
        @NamedNativeQuery(
                name = DictItemDAOBean.GET_ALL_DICT_ITEM,
                query = "SELECT * FROM T_SYS_DICT_ITEM WHERE VSTATUS != " + EntityObject.VSTATUS_REMOVED,
                resultClass = DictItem.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "dictmgmt")
                }),
        @NamedNativeQuery(
                name = DictItemDAOBean.GET_DICT_ITEMS_BY_CATEGORY_CODE,
                query = "SELECT * FROM T_SYS_DICT_ITEM WHERE VSTATUS=0 AND CATEGORY_CODE=$CATEGORY_CODE " +
                		"ORDER BY PRIORITY",
                resultClass = DictItem.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "dictmgmt")
                }),      
        @NamedNativeQuery(
                name = DictItemDAOBean.INSERT_DICT_ITEM,
                query = "INSERT INTO T_SYS_DICT_ITEM(" +
        		"ID, " +
        		"NAME, " +
        		"LOCAL_NAME, " +
        		"PRIORITY, " +
        		"VSTATUS, " +
        		"CREATE_TIME, " +
        		"CREATOR, " +
        		"LAST_MODIFIED, " +
        		"LAST_MODIFIER, " +
        		"DESCRIPTION, " +        		
        		"REFS," +
        		"VERSION," + 
        		"CATEGORY_CODE," +
        		"NUMBER_VALUE," +
        		"STRING_VALUE) " +
        		"VALUES(" +
        		"$DICTITEM.getId()," +
                "$DICTITEM.getName()," +
                "$DICTITEM.getLocalName()," +
                "$DICTITEM.getPriority()," +
                "$DICTITEM.getVstatus()," +
                "$DICTITEM.getCreateTime()," +
                "$DICTITEM.getCreator()," +
                "$DICTITEM.getLastModified()," +
                "$DICTITEM.getLastModifier()," +                        
                "$DICTITEM.getDescription()," +                
                "$DICTITEM.getRefs()," +
                "$DICTITEM.getVersion()," +                        
                "$DICTITEM.getCategoryCode()," +
                "$DICTITEM.getNumberValue()," +
                "$DICTITEM.getStringValue())",
                resultClass = DictItem.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "dictmgmt")
                }),      
        @NamedNativeQuery(
        		name=DictItemDAOBean.DELETE_DICT_ITEM,
        		query="UPDATE T_SYS_DICT_ITEM SET VSTATUS=" + EntityObject.VSTATUS_REMOVED + " WHERE ID=$ID",
        		resultClass = DictItem.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "dictmgmt")
                }),      
        @NamedNativeQuery(
        		name=DictItemDAOBean.UPDATE_DICT_ITEM,
                query = "UPDATE T_SYS_DICT_ITEM SET " +
        		"NAME=$DICTITEM.getName(), " +
        		"LOCAL_NAME=$DICTITEM.getLoacalName, " +
        		"PRIORITY=$DICTITEM.getPriority(), " +
        		"VSTATUS=$DICTITEM.getVstatus(), " +
        		"CREATE_TIME=$DICTITEM.getCreateTime(), " +
        		"CREATOR=$DICTITEM.getCreator(), " +
        		"LAST_MODIFIED=$DICTITEM.getLastModiied(), " +
        		"LAST_MODIFIER=$DICTITEM.getLastModifier(), " +
        		"DESCRIPTION=$DICTITEM.getDescription(), " +        		
        		"REFS=$DICTITEM.getRefs()," +
        		"VERSION=$DICTITEM.getVersion()," +    
        		"CATEGORY_CODE=$DICTITEM.getCategoryCode()," +
        		"NUMBER_VALUE=$DICTITEM.getNumberValue()," +
        		"STRING_VALUE = $DICTITEM.getStringValue()" +
        		" WHERE ID=$DICTITEM.getId()",
        		resultClass = DictItem.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "dictmgmt")
                	}
        		)
        }		
)
@Stateless
@Local
public class DictItemDAOBean extends SuperDAO implements DictItemDAO {

    public static final String GET_DICT_ITEM_BY_ID = "DictItemDAOBean.getDictItemById";
    public static final String GET_ALL_DICT_ITEM = "DictItemDAOBean.getAllDictItem";
    public static final String GET_DICT_ITEMS_BY_CATEGORY_CODE = "DictItemDAOBean.getDictItemsByCategoryCode";
    public final static String INSERT_DICT_ITEM = "DictItemDAOBean.insertDictItem";
    public final static String DELETE_DICT_ITEM = "DictItemDAOBean.deleteDictItem";
    public final static String UPDATE_DICT_ITEM = "DictItemDAOBean.updateDictItem"; 
 

    public DictItem getDictItemById(long id) {
        return (DictItem)getEntityObjectByColumn(GET_DICT_ITEM_BY_ID, "ID", id);
    }    

    public List<DictItem> getDictItemsByCategoryCode(String categoryCode) {
        Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("CATEGORY_CODE", categoryCode);
        return (List<DictItem>)processNamedNativeQuery(GET_DICT_ITEMS_BY_CATEGORY_CODE, params);
    }

    public List<DictItem> getAllDictItem() {
        return (List<DictItem>)processNamedNativeQuery(GET_ALL_DICT_ITEM, null);
    }

    public void insertDictItem(DictItem dictItem) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("DICTITEM", dictItem);
        executeNamedNativeUpdate(this.INSERT_DICT_ITEM, params);
    }

    public int deleteDictItem(long id) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("ID", id);
        return executeNamedNativeUpdate(this.DELETE_DICT_ITEM, params);
    }

    public int updateDictItem(DictItem dictItem) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("DICTITEM", dictItem);
        return executeNamedNativeUpdate(this.UPDATE_DICT_ITEM, params);
    }       
}
